using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using TeamCommunitySolution.Domain.DataLayer.Base;
using TeamCommunitySolution.Modules.Base;
using TeamCommunitySolution.Modules.Base.Requests;

namespace TeamCommunitySolution.Controllers
{
    internal class SystemUserController : Controller
    {

        /// <summary>
        /// User manager instance.
        /// </summary>
        BaseManager _manager = null;

        /// <summary>
        /// Initializes a new instance of the <see cref="SystemUserController"/> class.
        /// </summary>
        public SystemUserController()
            : this(MvcApplication.BaseService)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SystemUserController"/> class.
        /// </summary>
        /// <param name="service">The service.</param>
        public SystemUserController(BaseManager manager)
        {
            this._manager = manager;
        }

        /// <summary>
        /// Indexes this instance. GET: /SystemUser/
        /// </summary>
        /// <returns>Action Result.</returns>
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// Searches the name.
        /// </summary>
        /// <param name="q">The query string.</param>
        /// <returns>Action Result.</returns>
        [AcceptVerbs(HttpVerbs.Post | HttpVerbs.Get), Authorize]
        public ActionResult SearchName(string q)
        {

            SearchRequest request = new SearchRequest();
            request.SearchString = q;

            IList<SystemUser> userList = _manager.SearchSystemUser(request).ResultList;

            string[] users = new string[userList.Count];

            for (int i = 0; i < userList.Count; i++)
            {
                users[i] = string.Format("{0} {1} ({2})", userList[i].FirstName, userList[i].LastName, userList[i].NickName);
            }

            return Content(string.Join("\n", users));
        }
    }
}
